import { usePlayerStore, useToolStore } from '@/stores'
export const toTree = (data) => {
  const fixedArr = data.map((item) => {
    return {
      label: item.name,
      id: item.iD,
      visiblity: item.visiblity,
      index: item.index,
      parentIndex: item.parentIndex,
      color: item.color ? item.color.replace('RGB', 'rgb') : 'rgb(255, 255, 255)',
      style: item.style ? item.style : 0,
      type: item.type
    }
    // delete item.children;
  })
  const map = {}
  fixedArr.forEach(function (item) {
    map[item.index] = item
  })
  const val = []
  fixedArr.forEach(function (item) {
    const parent = map[item.parentIndex]
    if (parent) {
      ;(parent.children || (parent.children = [])).push(item)
    } else {
      val.push(item)
    }
  })
  return val
}
export function controlInfoTree(id, key, value, children) {
  const playerStore = usePlayerStore()
  const toolsStore = useToolStore()
  // cur 图层的id  key 要设置的属性  value 值
  // 操作显影
  if (key === 'visiblity') {
    // 操作图层
    if (value) {
      __g.infoTree.show(id)
    } else {
      __g.infoTree.hide(id)
    }
    const result = playerStore.TreeInfo.map((item) => {
      // console.log(item,"playerStoreitem")
      if (item.iD === id) {
        item[key] = value
        // if(item.ype==="EPT_Folder"){
        //
        // }
      }
      return item
    })
    // console.log(result);
    playerStore.TreeInfo(result)
    toolsStore.InfoTree(toTree(result))
  } else if (key === 'style') {
    const cur = playerStore.TreeInfo.find((item) => {
      return item.iD === id
    })
    // let { style, color } = value;
    // let styleObj = {}
    // 后者覆盖前者
    // if(style){
    //   styleObj.style = style
    // }else if(cur.style){
    //   styleObj.style = cur.style
    // }else{

    // }
    const styleObj = Object.assign(
      { style: 0, color: 'RGB(255,255,255)' },
      { style: cur.style, color: cur.color },
      value
    )
    // console.log({ style: 0, color: "RGB(255,255,255)" }, { style: cur.style, color: cur.color }, { style, color }, styleObj);
    // // 操作样式
    __g.tileLayer.setStyle(id, styleObj.style, styleObj.color)
    const result = playerStore.TreeInfo.map((item) => {
      if (item.iD === id) {
        item.style = styleObj.style
        item.color = styleObj.color
      }
      return item
    })
  }
  // 操作数据

  // store.commit("Data/infoTreeMock", result)
  // store.commit("Data/newInfotree", toTree(result))
}
